<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0">111</el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              class="login_btn"
              type="primary"
              size="default"
              style="width: 100%"
              @click="login"
            >
              登陆
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from "@element-plus/icons-vue";
import { ElNotification } from "element-plus";
import { reactive, ref } from "vue";
import useUserStore from "@/store/modules/user";
import { useRouter } from "vue-router";
import { getTime } from "@/utils/time";
//收集表单数据

let loginForm = reactive({ username: "admin", password: "111111" });
let useStore = useUserStore();
let $router = useRouter();
let loading = ref(false);
let loginForms = ref();
//表单校验规则
const validatorUsername = (rule: any, value: any, callback: any) => {
  // if (/^\d{5,10}$/.test(value)) {
  if (value.length >= 5) {
    callback();
  } else {
    callback(new Error("账号长度至少五位"));
  }
};
const validatorPassword = (rule: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback();
  } else {
    callback(new Error("密码长度至少六位"));
  }
};
const rules = {
  username: [
    // {
    //   required: true,
    //   min: 4,
    //   max: 10,
    //   message: "账号长度至少4位",
    // },
    {
      trigger: "change",
      validator: validatorUsername,
    },
  ],
  password: [
    // {
    //   required: true,
    //   min: 6,
    //   max: 10,
    //   message: "密码长度至少6位",
    //   trigger: "change",
    // },
    {
      trigger: "change",
      validator: validatorPassword,
    },
  ],
};
const login = async () => {
  //校验通过
  await loginForms.value.validate();
  loading.value = true;
  //发登陆请求
  try {
    await useStore.userLogin(loginForm);
    $router.push("/");
    ElNotification({
      type: "success",
      message: "欢迎回来",
      title: `HI，${getTime()}好`,
    });
    loading.value = false;
  } catch (error) {
    loading.value = false;
    ElNotification({
      type: "error",
      message: (error as Error).message,
    });
  }
};
</script>

<style lang="scss" scoped>
.login_container {
  height: 100vh;
  width: 100%;
  background-color: blueviolet;
  background-size: cover;
}
.login_form {
  position: relative;
  width: 80%;
  top: 30vh;
  background-size: cover;
  padding: 40px;
  h1 {
    color: #fff;
    font-size: 40px;
  }
  h2 {
    font-size: 20px;
    color: #fff;
    margin: 20px 0;
  }
}
</style>